<?php include('../includes/header.php');?>
<?php include(inc('sql.php'));?>
<?php include(inc('repository.php'));?>
<?php
	parameters_check($_REQUEST['id']);
	$group=NULL;
	$topic=NULL;
	$reply_total=0;
	if(isset($_REQUEST['id'])){
		$topic = Topic::get_by_id(intval($_REQUEST['id']));
		if($topic){
			$group = Group::get_by_id($topic['group_id']);
			$reply_total=Reply::getCount($topic['id']);
		}else{
			to_404();
		}
	}
	
?>
<div id="main">
	<span class="panel_title">
		<img src="<?php group_icon_url($group['id']) ?>" width="32" height="32" align="absmiddle"/>
		<a href="<?php group_topics_url($group['id'])?>"><?php e($group['name']) ?></a>
		<a href="<?php topic_compose_url($group['id'])?>" class="btn btn_new_post">发新贴</a>
	</span>
	<form action="/handlers/post_handler.php?action=save" method="post" id="new_topic_form">
	<div id="topic" class="content">
		<input type="hidden" value="<?php e($topic['id'])?>" id="data_topic_id"/>
		<div class="user">
			<?php
				$user = User::get_by_id($topic['user_id']);
			?>
			<div>
			<a href="<?php user_url($user['id']);?>" class="img_link">
				<img src="<?php avatar_url($user['id']) ?>" width="48" width="48" class="circle"/>
			</a>
			</div>
			<a href="<?php user_url($user['id']);?>" class="user_name">
			<?php e($user['name']) ?>
			</a>
		</div>
		<div class="post">
			<div class="title"><?php e($topic['title']);?></div>
			<div class="time">发布于<?php e(time_ago(strtotime($topic['created_at']))); ?></div>
			<pre class="content"><?php e($topic['content']);?></pre>
			<?php if($topic['updated_at']):?>
				<div class="updated_at grey_small">编辑于<?php e(time_ago(strtotime($topic['updated_at'])))?></div>
			<?php endif?>
			<?php if($topic['user_id']==user_val('id')):?>
			<div class="operation">
				<a href="<?php topic_edit_url($topic['id'])?>" class="edit_topic">编辑</a>
				<?php if($reply_total==0):?>
					<a href="javascript:void(0)" class="remove_topic">删除</a>
				<?php endif?>
			</div>
			<?php endif?>
		</div>
		<div class="clear"></div>
	</div>
	</form>
	<?php
		$page = isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$replies = Reply::get_by_topic_id($topic['id'],$page);
		if(is_array($replies)):
	?>
	<div id="replies" class="content">
		<?php 
			$index=RPP*($page-1);
			foreach ($replies as $reply):
				$index++;
		?>
		<div class="reply">
			<div class="user">
				<div>
				<a href="<?php user_url($reply['user_id']);?>" class="img_link">
					<img src="<?php avatar_url($reply['user_id']) ?>" width="48" width="48" class="circle"/>
				</a>
				</div>
				<a href="<?php user_url($reply['user_id']);?>" class="user_name">
				<?php e($reply['name']) ?>
				</a>
				<?php if($reply['user_id'] == $topic['user_id']):?>
				<div class="grey_small">楼主</div>
				<?php endif ?>
			</div>
			<div class="post" reply-id="<?php e($reply['id']) ?>">
				<a href="#<?php e($index)?>" class="grey_small"><?php e($index)?>楼#</a>
				<a name="<?php e($index)?>"></a>
				<span class="time">回复于<?php e(time_ago(strtotime($reply['created_at']))); ?></span>
				<pre class="content"><?php e($reply['content']);?></pre>
				<?php if($reply['updated_at']):?>
				<div class="updated_at grey_small">编辑于<?php e(time_ago(strtotime($reply['updated_at'])))?></div>
				<?php endif?>
				<?php if($reply['user_id']==user_val('id')):?>
				<div class="operation">
					<?php if($topic['allow_of_reply']):?>
						<a href="javascript:void(0);" class="edit_reply">编辑</a>
					<?php endif?>
					<a href="javascript:void(0);" class="remove_reply">删除</a>
				</div>
				<?php endif?>
			</div>
			<div class="clear"></div>
		</div>
		<?php endforeach?>
		<?php
		pagination($reply_total);
		?>
	</div>
	<?php endif ?>
	<?php if($topic['allow_of_reply']):?>
	<div id="new_reply" class="content">
		<div class="title">发表回复</div>
		<div class="user">
			<div>
			<a href="<?php user_url(user_val('id'));?>" class="img_link">
				<img src="<?php avatar_url(user_val('id')) ?>" width="48" width="48" class="circle"/>
			</a>
			</div>
			<a href="<?php user_url(user_val('id'));?>" class="user_name">
			<?php e(user_val('name')); ?>
			</a>
		</div>
		<div>
			<form action="/handlers/post_handler.php?action=reply" method="post" id="reply_form">
			<input type="hidden" name="topic_id" value="<?php e($topic['id']);?>" />
			<input type="hidden" name="id"/>
			<div>
				<textarea name="content" class="corner"></textarea>
			</div>
			<div class="response"></div>
			<input type="submit" value="回 复" class="btn" />
			</form>
		</div>
	</div>
	<?php endif ?>
</div>
<script type="text/javascript" src="/assets/javascripts/jquery.elastic.min.js"></script>
<?php include_once('group_sidebar.php')?>
<?php get_footer();?>